"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : 当前的硬件和网络环境不正常时检查操作系统
Create At   : 2021/3/3
Owner       : @qumin20145
Description :
    1.查询网卡：ifconfig | head -1
    2.进入cfe工具目录下注入网络故障
    3.在网络环境不正常的情况下检查操作系统：gs_checkos -i  A1 -h xxx
Expect      :
    1.获取到网卡信息
    2.注入网络故障成功
    3.故障时间结束返回结果
History     :
    Modified by @wan005: 修改断言匹配最新cfe
"""

import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '----Opengauss_Reliability_Tools_Gs_Checkos_Case008----')
        self.ProotNode = Node('PrimaryRoot')
        self.SrootNode = Node('Standby1Root')
        self.Constant = Constant()
        self.com = Common()

        status = Primary_SH.getDbClusterStatus('detail')
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)

    def test_server_tools(self):
        self.log.info('----1.查询网卡信息：获取网卡----')
        network_card_cmd = 'ifconfig | head -1'
        self.log.info(network_card_cmd)
        network_card_result = self.ProotNode.sh(network_card_cmd).result()
        dev_name = network_card_result.split(':')[0].strip()
        self.log.info(network_card_result)
        hostname1_cmd = f'''hostname;'''
        hostname1 = self.ProotNode.sh(hostname1_cmd).result()
        self.log.info(hostname1)

        self.log.info('----2.注入故障:主机网卡1分钟----')
        start_time = time.time()
        self.log.info(start_time)
        inject_cmd = f'rNet_down (dev, time)values({dev_name}, 60)'
        self.log.info(inject_cmd)
        inject_result = self.com.cfe_inject(self.ProotNode, inject_cmd)
        self.log.info(inject_result)
        self.assertIn(self.Constant.cfe_inject_netdown_success_msg,
                      inject_result)

        self.log.info('----3.当前的硬件和网络环境不正常时检查操作系统:故障时间结束返回结果----')
        startcheckos_time = time.time()
        self.log.info(startcheckos_time)
        checkos_cmd = f'''source {macro.DB_ENV_PATH};
            gs_checkos  -i  A1  -h {hostname1};
            '''
        self.log.info(checkos_cmd)
        checkos_msg = self.SrootNode.sh(checkos_cmd).result()
        self.log.info(checkos_msg)
        self.assertIn('A1', checkos_msg)
        end_time = time.time()
        self.log.info(end_time)

        self.log.info('--通过时间差校验步骤和结果的正确性--')
        self.log.info('start_time:' + str(start_time) +
                      ';startcheckos_time:' + str(startcheckos_time) +
                      ';end_time:' + str(end_time))
        self.log.info('start_time:' + str(start_time) +
                      ';startcheckos_time:' + str(startcheckos_time) +
                      ';end_time:' + str(end_time))

        if startcheckos_time - start_time < 60:
            self.log.info('----gs_checkos工具是在注入故障的60秒内执行的----')
            if end_time - start_time > 60:
                self.log.info('----gs_checkos工具执行结果是在注入故障时间超过60秒后才返回的----')
            else:
                self.log.info('----gs_checkos工具执行结果是在注入故障的60秒之内返回的，存在问题----')
        else:
            self.log.info('----gs_checkos工具是在注入故障故障时间超过60秒后执行的----')

    def tearDown(self):
        self.log.info('------------无需清理环境------------')
        self.log.info('Opengauss_Reliability_Tools_Gs_Checkos_Case008finish')
